﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace SplitScreenDemo
{
    public partial class frmMain : Form
    {
        public RefreshParentHandler<string> RefreshParent;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var frmChild = Application.OpenForms["frmChild"];
            if (frmChild != null)
            {
                frmChild.Activate();
            }
            else
            {
                frmChild frm = new frmChild();
                frm.Owner = this;//申明当前窗体是子窗体
                ShowOnMonitor(frm);
                frm.Show();
            }
        }

        private void ShowOnMonitor(frmChild frm)
        {
            Screen[] sc = Screen.AllScreens;
            if (sc.Length > 1)
            {
                //获取当前屏幕
                Screen CurrentScreen = Screen.FromControl(this);
                //获取当前鼠标所在的屏幕
                //Screen CurrentScreen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
                var child = sc.Where(it => it.DeviceName != CurrentScreen.DeviceName).FirstOrDefault();
                frm.StartPosition = FormStartPosition.Manual;
                frm.Location = new Point(child.Bounds.Left, child.Bounds.Top);

            }
            // If you intend the form to be maximized, change it to normal then maximized.  
            frm.WindowState = FormWindowState.Normal;
            frm.WindowState = FormWindowState.Maximized;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(textBox1.Text))
            {
                if (RefreshParent != null)
                    RefreshParent(textBox1.Text);
            }
        }
    }
}
